package org.xx.armory.docs;

import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;

/**
 * 段落元素, 段落元素属于块级文档元素。
 *
 * @author Haart
 */
public final class Paragraph
        extends Block
        implements Collection<Inline>, Serializable {
    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_CENTER = 1;
    public static final int ALIGN_RIGHT = 2;
    private static final long serialVersionUID = 1L;
    private final Deque<Inline> children;

    /**
     * 构造段落元素。
     *
     * @param className
     *         段落元素的类名, 会被正规化。
     */
    public Paragraph(
            String className
    ) {
        super(className);

        this.children = new ArrayDeque<>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int size() {
        return this.children.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isEmpty() {
        return this.children.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean contains(Object o) {
        return this.children.contains(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<Inline> iterator() {
        return this.children.iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object[] toArray() {
        return this.children.toArray();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("SuspiciousToArrayCall")
    public <T> T[] toArray(final T[] a) {
        return this.children.toArray(a);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean add(
            Inline e
    ) {
        if (e == null) {
            return false;
        } else if (e instanceof Text) {
            final Text t = (Text) e;
            if (t.getText() == null || t.getText().length() == 0) {
                return false;
            }
        }

        return this.children.add(e);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean remove(final Object o) {
        return this.children.remove(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean containsAll(final Collection<?> c) {
        return this.children.containsAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean addAll(
            Collection<? extends Inline> c
    ) {
        return this.children.addAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removeAll(final Collection<?> c) {
        return this.children.removeAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean retainAll(final Collection<?> c) {
        return this.children.retainAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear() {
        this.children.clear();
    }

    /**
     * 获取并移除最后一个文档元素。
     *
     * @return 最后一个文档元素, 如果不存在文档元素则返回{@code null}。
     */
    public final Inline pollLast() {
        return this.children.pollLast();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("<p class=\"").append(this.getClassName()).append("\">");
        for (final Inline e : this.children) {
            sb.append(e.toString());
        }
        sb.append("</p>");

        return sb.toString();
    }
}
